// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import $ from 'jquery'
import Vue from 'vue'
import App from './App'
import Weui from 'weui'
import router from './router'
import VueLazyload from 'vue-lazyload'

//公共样式
import "./assets/common/css/common.css"
import './assets/common/css/iconfont/iconfont.css'

//fastclick：处理移动端click事件300毫秒延迟
// const FastClick = require('fastclick')
// FastClick.attach(document.body)
import {vConsole} from "@/assets/common/js/vConsole";
//vux组件
import {
  LoadingPlugin,
  ToastPlugin,
  AlertPlugin,
  ConfirmPlugin,
  dateFormat,
  Search,
  Group,
  Cell,
  XButton
} from 'vux'

Vue.use(ConfirmPlugin)
Vue.use(AlertPlugin)
Vue.use(ToastPlugin)
Vue.use(LoadingPlugin)
Vue.component('search', Search)
Vue.component('group', Group)
Vue.component('cell', Cell)
Vue.component('x-button', XButton)
//图片的懒加载
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: require('@/assets/img/default.png'),   //请求失败后显示的图片
  loading: require('@/assets/img/default.png'),   //加载的loading过渡图片
  attempt: 1     // 加载图片数量
})


// function connectWebViewJavascriptBridge(callback) {
//   var u = navigator.userAgent;
//   var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;  // android终端
//   var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);  // ios终端
//   if (isAndroid) {
//       // Android使用
//       if (window.WebViewJavascriptBridge) {
//           callback(WebViewJavascriptBridge);
//       } else {
//           document.addEventListener(
//               'WebViewJavascriptBridgeReady'
//               , function () {
//                   callback(WebViewJavascriptBridge);
//               },
//               false
//           );
//       }
//   }
//   if (isiOS) {
//       // iOS使用
//       if (window.WebViewJavascriptBridge) {
//           return callback(window.WebViewJavascriptBridge);
//       }
//       if (window.WVJBCallbacks) {
//           return window.WVJBCallbacks.push(callback);
//       }
//       window.WVJBCallbacks = [callback];
//       var WVJBIframe = document.createElement('iframe');
//       WVJBIframe.style.display = 'none';
//       WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
//       document.documentElement.appendChild(WVJBIframe);
//       setTimeout(function () {
//           document.documentElement.removeChild(WVJBIframe);
//       }, 0);
//   }
// }

// connectWebViewJavascriptBridge(function (bridge) {
//   bridge.init(function (message, responseCallback) {
//       console.log('JS got a message', message);
//       var data = {
//           'Javascript Responds': '测试中文!'
//       };
//       if (responseCallback) {
//           console.log('JS responding with', data);
//           responseCallback(data);
//       }
//   });
//   bridge.registerHandler("functionInJs", function (data, responseCallback) {
//       document.getElementById("show").innerHTML = ("data from Java: = " + data);
//       if (responseCallback) {
//           var responseData = "Javascript Says Right back aka!";
//           responseCallback(responseData);
//       }
//   });
// })



// 注册一个全局自定义指令 `v-drag`
Vue.directive('drag', {
  // 当被绑定的元素插入到 DOM 中时……
  bind: function (el) {
    // 聚焦元素
    let odiv = el; //获取当前元素
    odiv.ontouchstart = (e) => {
      //算出鼠标相对元素的位置
      let disX = e.touches[0].clientX - odiv.offsetLeft;
      let disY;
      if (odiv.classList[1] == 'one') {
        disY = e.touches[0].clientY - odiv.offsetTop;
      }else{
        disY = e.touches[0].clientY - odiv.offsetTop;
      }

      odiv.ontouchmove = (e) => {
        //用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
        let left = e.touches[0].clientX - disX;
        let top = e.touches[0].clientY - disY;

        //绑定元素位置到positionX和positionY上面
        e.positionX = top;
        e.positionY = left;
        odiv.style.position = 'relative'
        //移动当前元素
        if (left <= 0) left = 0;
        if (left >= document.documentElement.clientWidth - odiv.offsetWidth) left = document.documentElement.clientWidth - odiv.offsetWidth;
        if (odiv.classList[1] == 'one') {
          if (top <= 0) top = 0;
          if (top >= document.documentElement.clientHeight - odiv.offsetHeight) top = document.documentElement.clientHeight - odiv.offsetHeight;
        } else {
          if (top <= 0) top = 0;
          if (top >= document.documentElement.clientHeight - odiv.offsetHeight) top = document.documentElement.clientHeight - odiv.offsetHeight;
        }

        odiv.style.left = left + 'px';
        odiv.style.top = top + 'px';
        if (e.type == 'touchmove') {
          e.preventDefault();
        }
      };

      odiv.ontouchend = (e) => {
        odiv.touchmove = null;
        odiv.touchend = null;
      };
    };
  }
})



//给每个页面设置标题
router.afterEach(route => {
  // 从路由的元信息中获取 title 属性
  if (route.meta.title) {
    document.title = route.meta.title;
    // 如果是 iOS 设备，则使用如下 hack 的写法实现页面标题的更新
    if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
      const hackIframe = document.createElement('iframe');
      hackIframe.style.display = 'none';
    }
  }
});

//配置
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {
    App
  },
  template: '<App/>'
})
